****************************************************************
* Replication do-file for the paper "Institutional constraints *
* on cabinet formation: Veto points and party system dynamics" * 
****************************************************************


**********************************************************************
* Create final dataset for analyis of cabinet types.                 *
* Documentation of dataset generation in ParlGov and Online-Appendix *
**********************************************************************

cd ""
clear
set more off

import delimited "cabinet-type-raw.csv", clear


** Case selection

* Keep only parliamentary systems
drop if country == "CYP" | country == "CHE" 

* remove the first election
generate temp_election_date = date(election_date, "YMD")
bysort country: egen temp_first = min(temp_election_date)
drop if temp_first == temp_election_date
drop temp_*


*remove caretaker cabinets
drop if caretaker==1

** Recoding and generating variables

* categorical variable `cabinet` type
encode cabinet_type, generate(cabinet)

* `majority` party in parliament (binary)
generate majority = 0
replace majority = 1 if seats_max_share > 50

* seats share radical (Communist and Right) parties
generate share_radical = share_com + share_right

* semi-presidentialism renaming -- see `pres_elec` for elected presidents
generate semi_pres = semi_pres_power

* `supermajorities` as binary variable -- only legislative veto ignoring constitutional veto
generate supermaj_1 = 0
replace supermaj_1 = 1 if supermajorities == 2

* recode polarization to precentage -- resonable coefficients in models
replace polarization = polarization * 100

* veto-point index -- max. 5
generate temp_bicam_1 = bicameralism_power
recode temp_bicam_1 (3=2) 
generate veto_points = pres_elec + semi_pres_power + temp_bicam_1 + investiture + supermaj_1 

drop temp_*


* label variables
label variable investiture "Investiture vote"
label variable bicameralism_binary "Bicameralism"
label variable semi_pres "Semi-presidential"
label variable caretaker "Caretaker"
label variable country_cee "CEE-country"
label variable majority "Majority situation"
label variable veto_points "Veto-points"
label variable en_parties "Eff. no. of parties"
label variable polarization "Polarization"
label variable share_radical "Seat share, radical parties"
label variable median_largest "Median-largest"
label variable seats_max_share "Seats share, largest party"


* Save dataset used in analysis

* All parliamentary and semi-presidential systems (used in the appendix)
saveold cabinet-type_all.dta, replace
export delimited using "cabinet-type_all.csv", replace

* Remove observations with mainly majority parties after elections (dataset used in the paper)
drop if country == "GBR" | country == "MLT"
drop if country == "NZL" & election_date < "1996-01-01"
drop if country == "ISR" & year>1995 & year<2004

saveold cabinet-type.dta, replace
export delimited using "cabinet-type.csv", replace

* check variables used in analysis
summarize caretaker cabinet_seats-veto_points




***************************************************
*  Install necessary Stata packages for the plots *
***************************************************

capture {
	net install estout.pkg
	ssc install combomarginsplot
	ssc install grstyle
	ssc install scheme_tufte
}

***********************
* EMPIRICAL ANALYSIS  *
***********************

** TABLE 2 - Estimates for the probability of type of cabinet formed (Multinomial logit model)

global controls en_parties polarization median_largest seats_max_share share_radical country_cee majority post_election

*Model 1 - additive
mlogit cabinet investiture bicameralism_binary semi_pres $controls, baseoutcome (1) nolog robust rrr
est store model1, title (Model 1)

*Model 2 - additive
mlogit cabinet veto_points $controls, baseoutcome (1) nolog robust rrr
est store model2, title(Model 2)

*Model 3 - interactive
mlogit cabinet  c.veto_points##c.polarization $controls, baseoutcome (1) nolog robust rrr
est store model3, title(Model 3)

esttab model1 model2 model3 using Table_2.rtf, replace se noomitted unstack s(ll chi2 r2_p N) label eform varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one


** FIGURE 2 - predicted probabablities for the veto-point index (based on model 2 in table 2)
quietly mlogit cabinet veto_points $controls, baseoutcome (1) nolog robust rrr
quietly margins, at(veto_points=(0(1)5)) asbalanced  
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") plotop(msymbol(i)) plot2opts(lpattern(solid) lwidth(medthick)) plot3opts(lpattern(longdash) lwidth(medthick)) ///
	addplot(hist veto_points ,  width(1)  below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black) lpattern(dash)  ///
	legend(holes(1 2)) legend(order(5 `"Minority"'  6 `"Surplus"') row(1)) )
* Please note that MWC is manually removed!
save figure_2.gph, asis replace
*graph export figure_2.pdf, replace
*graph export figure_2.svg, replace
*graph export figure_2.wmf, replace
*graph export figure_2.png, replace 


** FIGURE 3 - Interaction effects plots (based on model 3 in table 2)
quietly mlogit cabinet  c.veto_points##c.polarization $controls, baseoutcome (1) nolog robust rrr
margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(2))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Minority) yline(0) ///
addplot(hist veto_points if cabinet ==2, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file1, replace)

margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(3))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist veto_points if cabinet ==3, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file2, replace)

graph combine file1.gph file2.gph,  scheme(tufte) saving(figure_3.gph, replace) ycommon
*graph export figure_3.pdf, replace
*graph export figure_3.svg, replace
*graph export figure_3.wmf, replace
*graph export figure_3.png, replace 
 

 
** APPENDIX** 

**TABLE A1: List of variables 
quietly estpost summarize investiture bicameralism_binary semi_pres veto_points en_parties polarization share_radical median_largest seats_max_share  country_cee majority post_election
esttab, cells("count mean sd min max")
  
** FIGURE A1.  Contour plots of predicted probabablities 
use cabinet-type.dta, clear
quietly mlogit cabinet c.veto_points##c.polarization en_parties share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust rrr
quietly margins, at(veto_points =(0(1)5) polarization =(17(5)77)) asbalanced predict(outcome(2)) saving(predictions_min, replace)
quietly margins, at(veto_points =(0(1)5) polarization =(17(5)77)) asbalanced predict(outcome(3)) saving(predictions_sur, replace)

use predictions_min, clear
rename _at1 Veto_points
rename _at2  Polarization
rename _margin Pr_minority
twoway  (contour Pr_minority Veto_points Polarization, ccuts(0(0.1)1.0)), xlabel(20(10)80) xtitle("Polarization index") ytitle("Veto-point index") ztitle("Predicted probability")  title("Minority")               
graph save figure_app1.gph, replace

use predictions_sur, clear
rename _at1 Veto_points
rename _at2  Polarization
rename _margin Pr_minority
twoway  (contour Pr_minority Veto_points Polarization, ccuts(0(0.1)1.0)), xlabel(20(10)80) xtitle("Polarization index") ytitle("Veto-point index") ztitle("Predicted probability")  title("Surplus") 
graph save figure_app2.gph, replace

graph combine figure_app1.gph figure_app2.gph,  scheme(tufte) saving(figure1_appendix.gph, replace)  col(1) 
 
************************************************************************************************************************************************************************************************************


*********************
* Online Appendix *
*********************


set more off
use cabinet-type.dta, clear

**************************************************
* TABLE 6 -  Corresponds to table 2 in the paper *                                           *
**************************************************
sort cabinet_id
merge cabinet_id using appendix_data.dta
drop _merge

capture drop veto_points_alt

generate temp_bicam_1 = bicameralism_power
recode temp_bicam_1 (3=2) 
generate veto_points_alt = pres_elec + upper_majority +semi_pres_power+ cab_pres_party+ temp_bicam_1 + investiture + supermaj_1 
drop temp_*


* Model 1a -additive
mlogit cabinet investiture  upper_majority  cab_pres_party en_parties polarization share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust rrr
est store model1a, title (Model 1A)

* Model 1b -additive
mlogit cabinet investiture bicameralism_power upper_majority semi_pres  cab_pres_party en_parties polarization share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust rrr
est store model1b, title (Model 1B)

*Model 2 - additive
mlogit cabinet veto_points_alt en_parties polarization share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust rrr
est store model2, title(Model 2)
mlogit

*Model 3 - interactive
mlogit cabinet  c.veto_points_alt##c.polarization en_parties share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust rrr
est store model3, title(Model 3)

esttab model1a model1b model2 model3 using Table_A6.rtf, replace se noomitted unstack s(ll chi2 r2_p N) label eform varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one


** FIGURE 1 - Two marginal effects plots for the interaction effects (based on model 3 in table 2 in the paper)

mlogit cabinet  c.veto_points##c.polarization $controls, baseoutcome (1) nolog robust
margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(2))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte)  ytitle("") title(Minority) yline(0) ///
addplot(hist polarization if cabinet ==2, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black))  ytitle("") saving(file3, replace)

margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(3))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist polarization if cabinet ==3, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file4, replace)

graph combine file3.gph file4.gph ,  scheme(tufte) saving(figure_A1.gph, replace) ycommon
*graph export figure_A1.pdf, replace
*graph export figure_A1.svg, replace
*graph export figure_A1.wmf, replace
*graph export figure_A1.png, replace 




*************************************************************************
* TABLE 7 -  Corresponds to table 2 in the paper using                 *
* 1) Multinominal probit and 2) 2-level hierarchical multinominal logit *                                           *
*************************************************************************

** Model 1-2 corresponds to model 1 in table 2 in the paper
*Model 1 - additive, instituions -  Multinominal probit
mprobit cabinet investiture bicameralism_power semi_pres polarization  en_parties share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust
est store OA1, title (Multinominal probit)

* Model 2 - additive, institutions - 2-level multinominal logit
gsem (i.cabinet <- investiture bicameralism_power semi_pres polarization en_parties share_radical median_largest seats_max_share country_cee majority i.post_election M1[election_id]@1), mlogit vce(robust)
est store OA2, title (2-level multinominal logit)


** Model 3-4 corresponds to model 2 in table 2 in the paper
*Model 3 - additive, veto points -  Multinominal probit
mprobit cabinet veto_points  polarization en_parties share_radical median_largest seats_max_share country_cee majority post_election, baseoutcome (1) nolog robust
est store OA3, title(Multinominal probit)

* Model 4 - additive, veto points - 2-level multinominal logit
gsem (i.cabinet <- veto_points polarization en_parties  share_radical median_largest seats_max_share country_cee majority i.post_election M1[election_id]@1), mlogit vce(robust)
est store OA4, title (2-level multinominal logit)

esttab OA1 OA3 using Oneline_table_3A.rtf, replace se noomitted unstack s(ll chi2 r2_p N) varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one

esttab OA2 OA4 using Oneline_table_3B.rtf, replace se noomitted unstack s(ll chi2 r2_p N) label eform varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one


*************************************************************************
* Figure 2 -  Corresponds to figure 3 and 4 in the paper using          *
* 1) Multinominal probit and 2) 2-level hierarchical multinominal logit * 
*************************************************************************

use cabinet-type.dta, clear

** Model 1. Multinominal probit

* Estimate all combinations of interaction effects
local controls en_parties share_radical median_largest seats_max_share country_cee majority post_election
mprobit cabinet  c.veto_points##c.polarization `controls', baseoutcome (1) nolog robust

margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(2))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte)  ytitle("") title(Minority) yline(0) ///
addplot(hist polarization if cabinet ==2, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black))  ytitle("") saving(filea1, replace)

margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(3))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist polarization if cabinet ==3, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(filea2, replace)

margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(2))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Minority) yline(0) ///
addplot(hist veto_points if cabinet ==2, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(filea3, replace)

margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(3))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist veto_points if cabinet ==3, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(filea4, replace)

graph combine filea1.gph filea2.gph filea3.gph filea4.gph,  scheme(tufte) title(Multinominal probit) saving(Mprobit.gph, replace) 


** Model 2. 2-level multilevel multinomial logit (with random effects at the election level)
gsem (i.cabinet <- c.veto_points##c.polarization en_parties share_radical median_largest seats_max_share country_cee majority i.post_election M1[election_id]@1), mlogit vce(robust)
margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(2.cabinet))

marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte)  ytitle("") title(Minority) yline(0) ///
addplot(hist polarization if cabinet ==2, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black))  ytitle("") saving(file1b, replace)

margins, dydx(veto_points) at(polarization=(20(5)65)) asbalanced predict(outcome(3.cabinet))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist polarization if cabinet ==3, legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file2b, replace)

margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(2.cabinet))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Minority) yline(0) ///
addplot(hist veto_points if cabinet ==2, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file3b, replace)

margins, dydx(polarization) at(veto_points=(0(1)5)) asbalanced predict(outcome(3.cabinet))
marginsplot,  recastci(rarea)  ciopts(lp(dash) fc(gs10)) scheme(tufte) ytitle("") title(Surplus) yline(0) ///
addplot(hist veto_points if cabinet ==3, discrete legend(off) below yaxis(2) yscale(alt axis(2)) percent fcolor(none) lcolor(black)) saving(file4b, replace)

graph combine file1b.gph file2b.gph file3b.gph file4b.gph,  scheme(tufte) title(2-level multinominal logit) saving(2MLM_logit.gph, replace) 

graph combine Mprobit.gph 2MLM_logit.gph, scheme(tufte) col(1) saving(Online_appendix_figure2.gph, replace)
*graph export figure_5.pdf, replace
*graph export figure_5.svg, replace
*graph export figure_5.wmf, replace
*graph export figure_5.png, replace 


********************************************************************
* TABLE 8 -  Corresponds to model 1 in table 2 in the paper using *
* 1) conditional logit/mcfadden choice model and 2) mixed logit    * 
********************************************************************

* Model 5 - Conditional logit - potential coalitions - corresponds to model 1 in table A2 in the paper

* Import and merge datasets
use cabinet-type.dta, clear
sort cabinet_id
saveold cabinet-type1.dta, replace

import delimited cabinet-formation.csv, clear
sort cabinet_id
merge cabinet_id using cabinet-type1.dta
drop if _merge ==1
drop _merge

gen median_largest2=1 if largest==1 & median==1
recode median_largest2 .=0


* Creating interactions with institutions/the veto point index
gen invest_min=1 if investiture==1 & minority==1
recode invest_min .=0
gen invest_sur=1 if investiture==1 & surplus==1
recode invest_sur .=0
gen bicam_min=1 if bicameralism_binary==1 & minority==1
recode bicam_min .=0
gen bicam_sur=1 if bicameralism_binary==1 & surplus==1
recode bicam_sur .=0
gen semi_min=1 if semi_pres==1 & minority==1
recode semi_min .=0
gen semi_sur=1 if semi_pres==1 & surplus==1
recode semi_sur .=0
gen veto_min= veto_points*minority
gen veto_sur= veto_points*surplus

** Model 5 - Conditional logit - potential coalitions 
clogit formed invest_min invest_sur bicam_min bicam_sur semi_min semi_sur minority surplus median_largest2, group(cabinet_id ) vce(robust) or
est store OA5, title (Conditional logit)

** Model 6 - Mixed logit - potential coalitions 
* Please note that this model is computationally complex and therefore takes a very long time to estimate.

capture ssc install mixlogit

mixlogit formed  median_largest2, rand(invest_min invest_sur bicam_min bicam_sur semi_min semi_sur minority surplus) group(cabinet_id) id(election_id) nrep(50)
est store OA6, title (Mixed logit)

esttab OA5 OA6 using Oneline_table_4A.rtf, replace se eform noomitted unstack s(ll chi2 r2_p N) varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one

********************************************************************
* TABLE 9 -  Corresponds to model 2 in table 2 in the paper using *
* 1) conditional logit/mcfadden choice model and 2) mixed logit    * 
********************************************************************

** Model 7 - Conditional logit - potential coalitions 
clogit formed veto_min veto_sur minority surplus median_largest2, group(cabinet_id ) vce(robust) or
est store OA7, title (Conditional logit)

** Model 8 - Mixed logit - potential coalitions 
mixlogit formed median_largest2, rand(veto_min veto_sur minority surplus) group(cabinet_id ) id(election_id) nrep(50)
est store OA8, title (Mixed logit)

esttab OA7 OA8 using Oneline_table_4B.rtf, replace se eform noomitted unstack s(ll chi2 r2_p N) varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one



**************************************************************
** TABLE 10 - Sample including UK, Malta and NZ before 1996 **
**************************************************************

use cabinet-type_all.dta, clear

gen FPTP=0
replace FPTP=1 if country =="AUS" | country =="CAN" | country =="GBR" | (country == "NZL" & year<1996)


global controls en_parties polarization share_radical median_largest seats_max_share country_cee majority post_election FPTP

*Model 1 - additive
mlogit cabinet investiture bicameralism_binary semi_pres $controls, baseoutcome (1) nolog robust rrr
est store m10_1

*Model 2 - additive
mlogit cabinet veto_points $controls, baseoutcome (1) nolog robust rrr
est store m10_2

*Model 3 - interactive
mlogit cabinet  c.veto_points##c.polarization $controls, baseoutcome (1) nolog robust rrr
est store m10_3

esttab m10_1 m10_2 m10_3 using Table_A10.rtf, replace se noomitted unstack s(ll chi2 r2_p N) label eform varlabel(_cons Constant) star(* 0.10 ** 0.05 *** 0.01) one
 
 
